package com.hotchpotch.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.bean.CategoryChildBean;

import java.util.List;

/**
 * Created by Administrator on 2017/5/17 0017.
 */

public class ClassificationRightGridViewAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<CategoryChildBean.ChildBean> list;

    public ClassificationRightGridViewAdapter(Context context, List<CategoryChildBean.ChildBean> list) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_classification_right_gridview, null);
            viewHolder.imageViewIcon = (ImageView) convertView.findViewById(R.id.item_classification_right_grid_icon);
            viewHolder.textViewTitle = (TextView) convertView.findViewById(R.id.item_classification_right_grid_title);
            viewHolder.layoutLine = (LinearLayout) convertView.findViewById(R.id.item_classification_right_grid_line);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CategoryChildBean.ChildBean childBean = list.get(position);
//        Glide.with(context).load(childBean.cat_icon).placeholder(R.drawable.no_image).crossFade(800).diskCacheStrategy(DiskCacheStrategy.RESULT).into(viewHolder.imageViewIcon);
        viewHolder.textViewTitle.setText(childBean.cat_name);
        setLine(position, viewHolder.layoutLine);
        return convertView;
    }

    private void setLine(int position,View view) {
        int i = 0;
//        i = list.size() % 3;
        i = getCount() % 3;
        if (position + i + 1 > getCount()) {
            //最后一行分割线隐藏
            view.setVisibility(View.GONE);
        }
    }

    private class ViewHolder {
        ImageView imageViewIcon;
        TextView textViewTitle;
        LinearLayout layoutLine;
    }
}
